/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;


/**
 *
 * @author jotass
 */
public class mapa {
        public int w,h;
        
        private BufferedImage img;
        private Color colorAux;
        private int [][]matrix;
        private boolean terminado;
        
        mapa(){
            w = h = 0;
            
        }
        
        mapa(File s){
            
            try {
                img = ImageIO.read(s);
            } catch (IOException e) {
                System.out.println("No se puede leer la imagen");
            } // TODO code application logic here
            w = img.getWidth();
            h = img.getHeight();
            terminado=false;


             matrix= new int[h][w];



             for(int i=0; i<h;i++){
                 for(int j = 0;j<w;j++){
                    colorAux=new Color(img.getRGB(i, j));
            
                    if(colorAux.getBlue()==255 && colorAux.getRed()==255 && colorAux.getGreen()==255)
                      matrix[j][i]=0;
                    else if(colorAux.getBlue()==0 && colorAux.getRed()==0 && colorAux.getGreen()==0)
                           matrix[j][i]=-1;
            
                     else matrix[j][i]=2;
                 }
                }
        }
        
        public boolean end(){
        	
        	return terminado;
        	
        }
        
        public void satelitefin(){
        	
        	terminado=true;
        	
        }
        
        public int getW(){
            return w;
        }
        public int getH(){
            return h;
        }
        
        public int getContent (int i, int j){
        	if ( i>=0 && j>=0 && i<h && j<w ){
        		//System.out.println("La posicion ["+i+"]["+j+"] tiene "+matrix[i][j]);
        		return matrix[i][j];
        	}
        	else{
	        	//Devolvemos obstáCULO
	        	return -1;
        	}
        }
        
        public ArrayList<Pair> solution(){
        	
            ArrayList<Pair> aux = new ArrayList();
   
            for(int i = 0; i<w; i++){
                for(int j=0; j<h; j++){
                    if(matrix[i][j]==2){
                    	Pair points= new Pair(0,0);
                        
                        points.setFirst(i);
                        points.setSecond(j);;
                        
                        aux.add(points);
 
                    }
                }
            }
            return aux;
        }
    /**
     * @param args the command line arguments
     */
    public void print(){
        for(int i=0; i<h;i++){
         for(int j = 0;j<w;j++){
              System.out.print(matrix[i][j]);
              System.out.print(" ");
        }
        System.out.println();
        }
    }
    
}

    /*public static void main(String[] args) throws IOException {
   
        File f =new File("/home/manu/DBA/map3.png");
        mapa img = new mapa(f);
        System.out.print(img.getH());
        System.out.println();
       // img.print();
        ArrayList<Pair> sol;
        sol = img.solution();
        
        for(int i=0; i<sol.size(); i++){
        	System.out.print(sol.get(i).getFirst());
        	System.out.print(" ");
        	System.out.print(sol.get(i).getSecond());  
        	System.out.print(" ");
        	
        }
       }
   
    }*/